package com.likui.myqq.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.likui.myqq.R;
import com.likui.myqq.util.SystemMethod;

/**
 * Created by helikui on 2015/9/28.
 */
public class TitleBarView extends RelativeLayout {
    private Context mContext;
    private Button btnLeft, btnRight, btnTitleLeft, btnTitleRight;
    private TextView tvCenter;
    private LinearLayout common_contact;

    public TitleBarView(Context context) {
        super(context);
        mContext = context;
        initView();
    }


    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.common_title_bar, this);
        btnLeft = (Button) findViewById(R.id.title_btn_left);
        btnRight = (Button) findViewById(R.id.title_btn_right);
        btnTitleLeft = (Button) findViewById(R.id.contact_group);
        btnTitleRight = (Button) findViewById(R.id.contact_all);
        tvCenter = (TextView) findViewById(R.id.title_txt);
        common_contact = (LinearLayout) findViewById(R.id.common_contact);
    }

    public void setCommonTitle(int leftVisibility, int centerVisibility, int center1Visibility, int rightVisibility) {
        btnLeft.setVisibility(leftVisibility);
        btnRight.setVisibility(rightVisibility);
        tvCenter.setVisibility(centerVisibility);
        common_contact.setVisibility(center1Visibility);
    }

    public void setBtnLeft(int icon, int txtRes) {
        Drawable img = mContext.getResources().getDrawable(icon);
        int height = SystemMethod.dip2px(mContext, 20);
        int width = img.getIntrinsicWidth() * height / img.getIntrinsicHeight();
        img.setBounds(0, 0, width, height);
        btnLeft.setText(txtRes);
        btnLeft.setCompoundDrawables(img, null, null, null);
    }

    public void setBtnLeft(int txtRes) {
        btnLeft.setText(txtRes);
    }

    public void setBtnRight(int icon) {
        Drawable img = mContext.getResources().getDrawable(icon);
        int height = SystemMethod.dip2px(mContext, 30);
        int width = img.getIntrinsicWidth() * height / img.getIntrinsicHeight();
        img.setBounds(0, 0, width, height);
        btnRight.setCompoundDrawables(img, null, null, null);
    }

    public void setTitleLeft(int resId) {
        btnTitleLeft.setText(resId);
    }

    public void setTitleRight(int resId) {
        btnTitleRight.setText(resId);
    }

    public void setTitleText(int resId) {
        tvCenter.setText(resId);
    }

    public void setBtnLeftClickListener(OnClickListener clickListener) {
        btnLeft.setOnClickListener(clickListener);
    }

    public void setBtnRightOnclickListener(OnClickListener clickListener) {
        btnRight.setOnClickListener(clickListener);
    }

    public void setPopWindow(PopupWindow mPopWindow, TitleBarView titleBarView) {
        mPopWindow.setBackgroundDrawable(new ColorDrawable(Color
                .parseColor("#E9E9E9")));
        mPopWindow.showAsDropDown(titleBarView, 0, 0);
        mPopWindow.setAnimationStyle(R.style.popwin_anim_style);
        mPopWindow.setFocusable(true);
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.update();

        setBtnRight(R.drawable.skin_conversation_title_right_btn_selected);
    }

    public Button getTitleLeft() {
        return btnTitleLeft;
    }

    public Button getTitleRight() {
        return btnTitleRight;
    }

    public void destroyView() {
        btnLeft.setText(null);
        btnRight.setText(null);
        tvCenter.setText(null);
    }


}
